package com.aimei.security.config;


import com.aimei.security.filter.UserSecurityFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author xiah
 * @date 2018/5/5 15:39
 */
@Configuration
@ComponentScan("com.aimei.security")
public class AimeiSecrityConfig extends WebSecurityConfigurerAdapter {
       public static final String FILTER_URL="/api/**/sec/**";
       @Autowired
       private AuthenticationProvider authenticationProvider;

       @Override
       protected void configure(AuthenticationManagerBuilder auth) {
              auth.authenticationProvider(authenticationProvider);
       }

       @Override
       protected void configure(HttpSecurity http) throws Exception {
              System.out.println(1233333);
              /*http.csrf().disable().antMatcher("/seca/").addFilterAt(getUserSecurityFilter(),
                      UsernamePasswordAuthenticationFilter
                              .class).authorizeRequests().anyRequest().permitAll();*/

              http.csrf().disable()
                      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                      .authorizeRequests().anyRequest().permitAll().and()
                      .addFilterAfter(getUserSecurityFilter(),
                              UsernamePasswordAuthenticationFilter.class);
       }

       public UserSecurityFilter getUserSecurityFilter() {
              UserSecurityFilter userSecurityFilter = new UserSecurityFilter(FILTER_URL);
              return userSecurityFilter;
       }
}
